package us.mooresr.webtroopmanager.client.ui;

import java.util.List;
import java.util.Set;

import us.mooresr.webtroopmanager.client.place.DashboardPlace;
import us.mooresr.webtroopmanager.client.place.EditPersonPlace;
import us.mooresr.webtroopmanager.client.ui.cells.PersonCell;
import us.mooresr.webtroopmanager.shared.PersonSummary;
import us.mooresr.webtroopmanager.shared.Privilege;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimpleCheckBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class PeopleListViewImpl extends Composite implements PeopleListView {
	private PeopleListView.Presenter presenter;
	
	private static PeopleListUiBinder uiBinder = GWT.create(PeopleListUiBinder.class);
	
	interface PeopleListUiBinder extends UiBinder<Widget, PeopleListViewImpl> {
		
	}
	
	@UiField
	Button editButton;
	
	@UiField
	Button deleteButton;
	
	@UiField
	Button trainingButton;
	
	@UiField
	Label statusLabel;
	
	@UiField
	Button addButton;
	
	@UiField
	Button backButton;
	
	@UiField
	SimpleCheckBox showInactive;
	
	@UiField
	ScrollPanel scrollPanel;
	
	@UiHandler("addButton")
	void onAddClick(ClickEvent event) {
		presenter.goTo(new EditPersonPlace(null));
	}
	
	@UiHandler("editButton")
	void onEditClick(ClickEvent event) {
		presenter.goTo(new EditPersonPlace(selectedPerson.getId()));
	}
	
	@UiHandler("deleteButton")
	void onDeleteClick(ClickEvent event) {
		presenter.deletePerson(selectedPerson);
	}
	
	@UiHandler("trainingButton")
	void onTrainingClick(ClickEvent event) {
	
	}
	
	@UiHandler("backButton")
	void onBackClick(ClickEvent event) {
		presenter.goTo(new DashboardPlace());
	}
	
	private CellList<PersonSummary> cellList;
	private ListDataProvider<PersonSummary> dataProvider;
	private PersonSummary selectedPerson;

	
	public PeopleListViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	
		ProvidesKey<PersonSummary> keyProvider = new ProvidesKey<PersonSummary>() {
			public Object getKey(PersonSummary item) {
				return (item == null) ? null : item.getId();
			}
		};
		cellList = new CellList<PersonSummary>(new PersonCell(), keyProvider);

		cellList.addStyleName("listControl");
		
		final SingleSelectionModel<PersonSummary> selectionModel = new SingleSelectionModel<PersonSummary>(keyProvider);
		cellList.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				selectedPerson = selectionModel.getSelectedObject();
				if (selectedPerson == null) {
					// Disable buttons that act on a selected person
					editButton.setEnabled(false);
					deleteButton.setEnabled(false);
					trainingButton.setEnabled(false);
				} else {
					// Enable buttons that act on a selected person
					if (presenter.isAllowed(Privilege.MODIFY_PERSON, selectedPerson.getId())) {
						editButton.setEnabled(true);
					}
					if (presenter.isAllowed(Privilege.DELETE_PERSON, selectedPerson.getId())){ 
						deleteButton.setEnabled(true);
					}
					if (presenter.isAllowed(Privilege.MODIFY_PERSON, selectedPerson.getId())){ 
						trainingButton.setEnabled(true);
					}
				}
				
				
			}
			
		});
		
		dataProvider = new ListDataProvider<PersonSummary>();
		dataProvider.addDataDisplay(cellList);
		
		scrollPanel.setWidget(cellList);

		deleteButton.setEnabled(false);
		editButton.setEnabled(false);
		trainingButton.setEnabled(false);

		
	}


	@Override
	public void setData(List<PersonSummary> data) {

		List<PersonSummary> providerList = dataProvider.getList();
		providerList.clear();
		for (PersonSummary p : data) {
			providerList.add(p);
		}
		cellList.setPageSize(data.size());
		
	}


	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
		if (presenter.hasPermission(Privilege.ADD_PERSON)) {
			addButton.setEnabled(true);
		} else {
			addButton.setEnabled(false);
		}
		
	}


	@Override
	public void addPerson(PersonSummary person) {
		List<PersonSummary> providerList = dataProvider.getList();
		providerList.add(person);
		
		// TODO: Resort the list?
		
		cellList.setPageSize(providerList.size());
	}


	@Override
	public void removePerson(Long deletedId) {
		List<PersonSummary> providerList = dataProvider.getList();
		for (int i=0; i<providerList.size(); i++) {
			PersonSummary ps = providerList.get(i);
			if (ps.getId() == deletedId) {
				providerList.remove(i);
				break;
			}
		}

		
	}
	
	@Override
	public void setStatus(String status) {
		statusLabel.setText(status);
		
	}
}
